import {saveOpenid} from '@/utils/localStorage.js';

const state = {
	hasLogin: false,
	userInfo: null,
	hasBindPhone: false,
	historySearch:null,
	isCancel:false,//订单是否取消
	isSubscribe:false, //是否订阅
	searchData:[], //研究方向
	token:'',
	permList:[], //权限列表
	mainAccount:true,
	isLine:false
}

const actions = {};


const mutations = {
	saveSearchData(state,searchData){
		state.searchData = searchData;
	},
	clearUserInfo(state, data){
		try{
			uni.removeStorageSync('userInfo');
			uni.removeStorageSync('openid');
			state.userInfo = null;
			state.hasLogin = false;
		}catch(e){
			//TODO handle the exception
		}
	},
	saveUserInfo(state, info){
		if(JSON.stringify(info) === '{}'){
			state.userInfo = {}
		}
		else{
			state.userInfo = Object.assign({} , state.userInfo , info);
		}
		// 本地存储
		try{			
			uni.setStorageSync('userInfo',state.userInfo);
		}catch(e){
			//TODO handle the exception
		}
		state.hasLogin = true;
		//判断 是否手机号 是否存在
		if(info.phone === null){
			uni.navigateTo({
				url:'/pagesB/registe-info/registe-info.vue'
			})
		}
		else{
			state.hasBindPhone = true;
		}
	},
	saveHistorySearch(state,data){
		state.historySearch = data
	},
	setLogin(state,loginState){
		state.hasLogin = loginState;
	},
	//设置是否删除状态
	SetIsCancel(state,data){
		state.isCancel = data
	},
	// 重置删除状态
	resetIsCancel(state){
		state.isCancel = false
	},
	//设置是否订阅
	SetIsSubscribe(state,data){
		state.isSubscribe = data
	},
	// 重置订阅状态
	resetIsSubscribe(state){
		state.isSubscribe = false
	},
	setToken(state,data){
		state.token = data;
	},
	//设置权限信息
	setPermList(state,data){
		state.permList = data;
	},
	//清空权限信息
	clearPermList(state,data){
		state.permList = '';
	},
	setMainAccount(state,data){
		state.mainAccount = data;
	},
	//设置是否订阅
	setIsLine(state,data){
		state.isLine = data;
	}
}

export default {
    namespaced: true,
    state,
    actions,
    mutations
} 